<?php

namespace Itwmw\Attributes\Validate;

use JetBrains\PhpStorm\ExpectedValues;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Rule
{
    /** @var int 普通规则 */
    public const TYPE_NORMAL = 0;

    /** @var int 隐式扩展规则 */
    public const TYPE_IMPLICIT = 1;

    /** @var int 依赖性规则 */
    public const TYPE_DEPENDENT = 2;

    public readonly array $args;

    /**
     * @param string|array                                                    $name    可调用方法
     * @param int<Rule::TYPE_NORMAL|Rule::TYPE_IMPLICIT|Rule::TYPE_DEPENDENT> $type    规则类型
     * @param mixed                                                           ...$args 传递给规则的参数
     */
    public function __construct(
        public readonly string|array $name,
        #[ExpectedValues(valuesFromClass: self::class)]
        public readonly int $type = self::TYPE_NORMAL,
        mixed ...$args,
    ) {
        $this->args = $args;
    }

    public function getRule(): string
    {
        if (!empty($this->args)) {
            return $this->name . ':' . implode(',', $this->args);
        }
        return $this->name;
    }
}
